package net.thxjj.adminServer.controller;

import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.entity.ResponseModel;
import net.thxjj.adminServer.entity.ResultEnum;
import net.thxjj.adminServer.service.IUserService;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/userManage")
public class UserController extends BaseController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private IUserService userService;


    /**
     * 获取管理员账号列表
     * @return
     */
    @RequestMapping(value="/userList",method= RequestMethod.GET)
    @ResponseBody
    public Object userList(){

        List<ParameterMap> list = userService.getUserList();

        return ResponseModel.getModel(ResultEnum.SUCCESS, list);

    }

    /**
     * 编辑管理员账号信息
     * 包括nickname,phone,email,age,sex,avatar
     * @return
     */
    @RequestMapping(value="/userEdit",method= RequestMethod.POST)
    @ResponseBody
    public Object userEdit(@RequestBody JSONObject jsonObject){


        if(!jsonObject.containsKey("user_id")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        if(!jsonObject.containsKey("nickname")
                && !jsonObject.containsKey("phone")
                && !jsonObject.containsKey("email")
                && !jsonObject.containsKey("age")
                && !jsonObject.containsKey("sex")
                && !jsonObject.containsKey("avatar")
        ){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        long user_id = jsonObject.getLongValue("user_id");
        if(!userService.isUserExist(user_id)){
            return ResponseModel.getModel(ResultEnum.ADMIN_NOT_EXIST_ERROR,null);
        }

        if(jsonObject.containsKey("age")){
            long age = jsonObject.getLongValue("age");
            if(age<=0 || age>200){
                return ResponseModel.getModel(ResultEnum.ADMIN_AGE_ERROR,null);
            }
        }

        if(jsonObject.containsKey("sex")){
            long sex = jsonObject.getLongValue("sex");
            if(sex<0||sex>2){
                return ResponseModel.getModel(ResultEnum.ADMIN_SEX_ERROR,null);
            }
        }
        String nickname = jsonObject.getString("nickname");
        String phone = jsonObject.getString("phone");
        String email = jsonObject.getString("email");
        String avatar = jsonObject.getString("avatar");
        String password = jsonObject.getString("password");

        ParameterMap parameterMap= new ParameterMap();
        parameterMap.put("user_id",user_id);
        parameterMap.put("nickname",nickname);
        parameterMap.put("phone",phone);
        parameterMap.put("email",email);
        parameterMap.put("avatar",avatar);
        parameterMap.put("password",password);
        if(jsonObject.containsKey("age")){
            parameterMap.put("age",jsonObject.getLongValue("age"));
        }
        if(jsonObject.containsKey("sex")){
            parameterMap.put("sex",jsonObject.getLongValue("sex"));
        }
        return userService.edit(parameterMap);

    }

    /**
     * 新增管理员账号信息
     * @return
     */
    @RequestMapping(value="/userAdd",method= RequestMethod.POST)
    @ResponseBody
    public Object userAdd(@RequestBody JSONObject jsonObject){



        if(!jsonObject.containsKey("username") || !jsonObject.containsKey("password")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }


        if(jsonObject.containsKey("age")){
            long age = jsonObject.getLongValue("age");
            if(age<0 || age>200){
                return ResponseModel.getModel(ResultEnum.ADMIN_AGE_ERROR,null);
            }
        }

        if(jsonObject.containsKey("sex")){
            long sex = jsonObject.getLongValue("sex");
            if(sex<0||sex>2){
                return ResponseModel.getModel(ResultEnum.ADMIN_SEX_ERROR,null);
            }
        }
        String username = jsonObject.getString("username");
        String nickname = jsonObject.getString("nickname");
        String phone = jsonObject.getString("phone");
        String email = jsonObject.getString("email");
        String avatar = jsonObject.getString("avatar");
        String password = jsonObject.getString("password");

        ParameterMap parameterMap= new ParameterMap();
        parameterMap.put("username",username);
        parameterMap.put("nickname",nickname);
        parameterMap.put("phone",phone);
        parameterMap.put("email",email);
        parameterMap.put("avatar",avatar);
        parameterMap.put("password",password);
        if(jsonObject.containsKey("age")){
            parameterMap.put("age",jsonObject.getLongValue("age"));
        }
        if(jsonObject.containsKey("sex")){
            parameterMap.put("sex",jsonObject.getLongValue("sex"));
        }
        return userService.add(parameterMap);

    }



    /**
     * 获取管理员角色列表
     * @return
     */
    @RequestMapping(value="/getRole",method= RequestMethod.GET)
    @ResponseBody
    public Object getRole(){

        String user_id = this.getParameterMap().getString("user_id");

        if(Tools.isEmpty(user_id)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        if(!Tools.isLongNumber(user_id)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
        }

        if(!userService.isUserExist(Long.parseLong(user_id))){
            return ResponseModel.getModel(ResultEnum.ADMIN_NOT_EXIST_ERROR,null);
        }

        return userService.getRole(this.getParameterMap());

    }

    /**
     * 编辑管理员角色
     * @return
     */
    @RequestMapping(value="/editRole",method= RequestMethod.POST)
    @ResponseBody
    public Object editRole(@RequestBody JSONObject jsonObject){

        if(!jsonObject.containsKey("user_id")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        long user_id = jsonObject.getLongValue("user_id");
        if(!userService.isUserExist(user_id)){
            return ResponseModel.getModel(ResultEnum.ADMIN_NOT_EXIST_ERROR,null);
        }



        try {
            String ad = jsonObject.getString("ids");
            ParameterMap pam = new ParameterMap();
            pam.put("user_id",user_id);
            if(!Tools.isEmpty(ad)){
                String[] aa = ad.split(",");
                boolean flag = false;
                for (int i=0;i<aa.length;i++){
                    if(!Tools.isLongNumber(aa[i])){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
                }
                pam.put("ids",jsonObject.getString("ids"));
            }else{
                pam.put("ids","");
            }

            return userService.editRole(pam);
        }catch (Exception e){
            return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
        }


    }

    /**
     * 删除管理员（状态设置为锁定）
     * @return
     */
    @RequestMapping(value="/deleteUser",method= RequestMethod.POST)
    @ResponseBody
    public Object deleteUser(@RequestBody JSONObject jsonObject){

        if(!jsonObject.containsKey("user_id")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        Long user_id = jsonObject.getLongValue("user_id");

        if(!userService.isUserExist(user_id)){
            return ResponseModel.getModel(ResultEnum.ADMIN_NOT_EXIST_ERROR,null);
        }

        if(user_id==1){
            return ResponseModel.getModel(ResultEnum.SUPER_ADMIN_CAN_NOT_LOCK_ERROR,null);
        }

        return userService.setUserLock(user_id);
    }

}
